package org.abettor.angleworm.ui;

import java.io.IOException;

import org.abettor.angleworm.i18n.Translator;
import org.abettor.angleworm.util.AgreementReader;
import org.abettor.util.Environment;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;

public class AgreementDialog
{
	private static Translator translator = null;
	private static final int WIDTH = 600;
	private static final int HEIGHT = 450;
	private boolean agreed = false;
	private Display display = Display.getDefault();
	private Shell mainShell = null;
	private Text textAgreement = null;
	private Button checkBox = null;
	private Button button = null;

	/**
	 * 显示声明款窗口
	 */
	public void show()
	{
		createMainShell();
		mainShell.open();
		while(! mainShell.isDisposed())
		{
			if(! display.readAndDispatch())
			{
				display.sleep();
			}
		}
	}

	/**
	 * 检查是否同意条款
	 */
	public boolean isAgreed()
	{
		return agreed;
	}

	/**
	 * 创建主窗口
	 */
	private void createMainShell()
	{
		try
		{
			translator = Translator.getInstance();
			if(translator == null)
			{
				translator = Translator.initialize("en_US");
			}
		}
		catch (IOException e)
		{
			e.printStackTrace();
			return;
		}

		GridLayout mainLayout = new GridLayout();
		mainLayout.horizontalSpacing = 10;
		mainLayout.verticalSpacing = 10;
		Monitor monitor = display.getPrimaryMonitor();
        Rectangle rect = monitor.getBounds();
        mainShell = new Shell(SWT.MIN | SWT.CLOSE);
        mainShell.setText(translator.getString("agreement.dialog.title"));
        mainShell.setImage(new Image(display, Environment.getFilePath("img/logo.png")));
        mainShell.setSize(new Point(WIDTH, HEIGHT));
        mainShell.setLocation((rect.width - WIDTH) / 2, (rect.height - HEIGHT) / 2);
        mainShell.setLayout(mainLayout);

        GridData gridDataText = new GridData();
        gridDataText.grabExcessHorizontalSpace = true;
        gridDataText.grabExcessVerticalSpace = true;
        gridDataText.horizontalAlignment = GridData.FILL;
        gridDataText.verticalAlignment = GridData.FILL;
        textAgreement = new Text(mainShell, SWT.BORDER | SWT.MULTI | SWT.WRAP | SWT.V_SCROLL | SWT.READ_ONLY);
        textAgreement.setLayoutData(gridDataText);
        textAgreement.setText(AgreementReader.getAgreement());

        GridData gridDataCheck = new GridData();
        gridDataCheck.grabExcessHorizontalSpace = true;
        gridDataCheck.horizontalAlignment = GridData.BEGINNING;
        checkBox = new Button(mainShell, SWT.CHECK);
        checkBox.setText(translator.getString("agreement.dialog.agree"));
        checkBox.setLayoutData(gridDataCheck);

        GridData gridDataButton = new GridData();
        gridDataButton.grabExcessHorizontalSpace = true;
        gridDataButton.horizontalAlignment = GridData.END;
        button = new Button(mainShell, SWT.NONE);
        button.setText(translator.getString("agreement.dialog.complete"));
        button.setLayoutData(gridDataButton);
        button.setEnabled(false);

        mainShell.addShellListener(new ShellAdapter()
        {
        	public void shellClosed(ShellEvent e)
        	{
        		agreed = false;
        	}
        });
        checkBox.addSelectionListener(new SelectionAdapter()
        {
        	public void widgetSelected(SelectionEvent e)
        	{
        		agreed = checkBox.getSelection();
        		if(agreed)
        		{
        			button.setEnabled(true);
        		}
        		else
        		{
        			button.setEnabled(false);
        		}
        	}
        });
        button.addSelectionListener(new SelectionAdapter()
        {
        	public void widgetSelected(SelectionEvent e)
        	{
        		agreed = checkBox.getSelection();
        		mainShell.dispose();
        	}
        });
	}
}
